#!/bin/sh
# the next line restarts using tclsh \
exec tclsh "$0" "$@"

# parse Braces ----------------------
puts "START BRACES" 

#set a b
#puts {a}
#puts {   
#   a   }   
#puts {{a}}
#puts { { a } }
#puts {$a}
#puts {"a"}
#puts {[a]}
#puts {a b}
#puts {{a} {b}}

#puts {a}a BAD
puts a{a}a
puts a{a}
puts a{a} 

# puts {a}} bad
# puts {{a} bad

# parse VarSubst-------------------------------

puts "START VARSUBST" 
set {a["x} b
puts ${a["x}
#puts ${a}
#puts $"a"
#puts $"{a}"
#puts $a"a
#puts $$
#puts $$a

#puts ${a}} will end at first }
#puts ${ bad
#puts ${${a}} Possobly bad strang name
#puts ${"a"} BAD Variable not found

# parse String ----------------

puts "START STRING" 
#puts a1}]"{}_b"
#set a b
#puts a
#puts a"a"
#puts a{a}
#puts a$a
#puts a${a}a
#puts a$a{
#puts a$"a"a
#puts a"$a"a

# parse Quotes ----------------------
puts "START QUOTES" 

#set a b
#puts "a"
#puts "a{a}"
#puts "a$a"
#puts "a{$a}"
#puts "a${a}"

#puts "aaa"aa"  #Bad must be space after

# parse words ---------------------
puts "START WORDS"


# parseCmdSubst ----------------------------------

puts "START CMD SUBST" 
puts [ expr  1 + 1 ]
puts [ expr  1 + 1]
#puts [ expr 1  + [ expr 1 + 1 ] ]
#puts [ expr  [ expr  1 + 1 ] + 1 ]
#puts [ expr [ expr 1 + 1 ] + [ expr  1 + 1 ] ]

#puts a[ expr  1 + 1 ]
#puts aa[ expr  1 + 1 ]
#puts aa[ expr  1 + 1 ]a
#puts [ expr  1 + 1 ]a


# expr command ----------------------------
puts "START EXPR" 
#expr 1 + 1 
#set a 1
#puts [incr a ]
#puts $a 

# puts command ----------------------------
puts "START PUTS" 
#puts [ set a 999 ]
#puts $a
#puts 123
#puts [ puts 123 ]

# if command ------------------------------
puts "START IF" 
#if { 1 == 0 } { puts true } { puts false }


#if { 1 == 0 } [puts hej] { 
#	puts lines
#}

if " 1 == 1 " " puts true " 

if " 1 == 1 " " 
	puts true 
	" 
if [ 1 == 1 ] " 
	puts true 
	" 

#set a true
#if { $a } {
#	puts varsubst
#}

#set a [ if { true } { puts hej } ]
#set b []
#puts $b
#puts [ if { true } { puts hej } ]
#set a 2 
#puts [ if { true } { incr a  } ]

#if { 1 == [expr 0 +1 ] } { puts hej2  
#} 

# Ok
#if [expr 0 + 1 ] [
#	puts hejXXX  
#]

# Ok
#set a "puts hejZZZ"
#if "222" { puts $a }



#if {true} { 
#puts true }

#set a 1
#set b 2
#if true { puts $a ;puts $b }

#set if "$a + 1"
#set a 5
#puts $if

# while command ------------------------------
puts "START WHILE" 
#set a 2
#puts [ while { $a < 5 } { incr a } ]

#while true {
#	puts [ gets stdin ]
	#set a [ gets stdin ]
	#puts $a
#}

# gets command ------------------------------
#set b [ gets stdin a ]
#puts $b


# proc command  ----------------------------------
puts "START PROC" 
#set a proc { x } {
#	puts $x 
#}


#proc if { x } {
#	puts $x 
#}


# reqursion   ----------------------------------

#proc My_Req_Proc { count } {
#	if { $count == 0 } {
#		puts $count
#		return 
#	}
#	puts $count
#	My_Req_Proc [ expr $count - 1 ]	
#}

#My_Req_Proc 1

proc My_Req_Proc { count } {
	if { $count == 0 } {
		return 0
	}
	return [ expr 1 + [ My_Req_Proc [ expr $count - 1 ]]]	
}

puts [ My_Req_Proc 3	 ]

# misc  ----------------------------------

#puts [if 5]

#set a  99

#puts aaa[ expr 1 + 2]bbb

#puts bbb[ expr 1 + 2 ]bb
#puts {bbb[ expr 1 + 2 ] bb}
#puts "bbb[ expr 1 + 2 ] bb" 
#puts [ expr 1 + 2 ]	
#puts [ expr 1 + [expr 2 +3 ] ]	



# Ok noting happends
#puts}	{ sda{s}da }
#puts { sda $a da }
#puts { sss [ expr 1 + 1 ] dss }
# Wrong
#puts { sss {a dss }
#puts { sss a} dss }

# Ok
#puts [ expr [ expr 1 + 2 ] + 4 ] 

#Ok
#puts "dsfd [ expr 1 + 2 ] ds"

# No nested "
#puts "sfsdf "sdf" sdf"

#set b puts
#$b hej

# \n kept !
#set {a}  {{
#99}} 
#{puts} $a






 	